/**
 * This file is part of 1genia trampoline
 * Copyright (C) 2007 1genia (contact@1genia.com)
 *
 * This library is free software; you can redistribute it and/or modify
 * it under the terms of the GNU Lesser General Public License as
 * published by the Free Software Foundation; version 3 of the License. 
 *
 * This library is distributed in the hope that it will be useful, but
 * WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Library General Public License for more details. 
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library; see the file COPYING.TXT.  If not,
 * write to the Free Software Foundation, Inc., 51 Franklin Street,
 * Fifth Floor, Boston, MA 02110-1301, USA. 
 **/
package com.genia.toolbox.persistence.hibernate.spring.factory;

import java.util.Set;

import org.hibernate.HibernateException;
import org.hibernate.event.DeleteEvent;
import org.hibernate.event.DeleteEventListener;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.genia.toolbox.basics.exception.BundledException;
import com.genia.toolbox.persistence.spring.manager.DependencyManager;

/**
 * implementation of {@link org.hibernate.DeleteEventListener} to allow the
 * {@link DependencyManager} to do its job.
 */
@SuppressWarnings("serial")
public class HibernateDeleteEventListener
    implements DeleteEventListener
{

  /**
   * the <code>Log</code> variabe.
   */
  private static final Logger LOGGER = LoggerFactory.getLogger(HibernateDeleteEventListener.class);

  /**
   * the {@link DependencyManager} to use.
   */
  private DependencyManager dependencyManager;



  /**
   * getter for the dependencyManager property.
   * 
   * @return the dependencyManager
   */
  public DependencyManager getDependencyManager()
  {
    return dependencyManager;
  }



  /**
   * setter for the dependencyManager property.
   * 
   * @param dependencyManager
   *          the dependencyManager to set
   */
  public void setDependencyManager(DependencyManager dependencyManager)
  {
    this.dependencyManager = dependencyManager;
  }



  /**
   * handle the given delete event.
   * 
   * @param event
   *          the delete event to be handled
   * @see org.hibernate.event.DeleteEventListener#onDelete(org.hibernate.event.DeleteEvent)
   */
  public void onDelete(DeleteEvent event)
  {
    try {
      getDependencyManager().onDelete(event.getObject());
    }
    catch (BundledException e) {
      LOGGER.error(e.getMessage(), e);
      throw new HibernateException(e);
    }
  }



  /**
   * handle the given delete event. This is the cascaded form.
   * 
   * @param event
   *          the delete event to be handled
   * @param transientEntities
   *          the cache of entities already deleted
   * @see org.hibernate.event.DeleteEventListener#onDelete(org.hibernate.event.DeleteEvent,
   *      java.util.Set)
   */
  public void onDelete(DeleteEvent event, @SuppressWarnings("unchecked")
  Set transientEntities)
  {
    onDelete(event);
  }

}
